﻿

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using MindTree.PerfWorkbench.Azure.Monitor.Core;
using MindTree.PerfWorkbench.Azure.Monitor.AzureMonitor;
using Microsoft.Research.DynamicDataDisplay.DataSources;
using Microsoft.Research.DynamicDataDisplay;
using MindTree.PerfWorkbench.Azure.Monitor.graphview;

namespace MindTree.PerfWorkbench.Azure.Monitor
{
    /// <summary>
    /// Interaction logic for GraphControl.xaml
    /// </summary>
    public partial class GraphControl : UserControl
    {
        //private GraphEntity graph = new GraphEntity();
        
        private GraphView view;

        public GraphView View
        {
            get { return view; }
            set { view = value; }
        }

        /// <summary>
        /// Default Constructor
        /// </summary>
        public GraphControl()
        {
            InitializeComponent();
            View = new GraphView();
            View.GraphEnt = new GraphEntity();
            View.GraphEntMetaData = new GraphEntityMetaData();
        }

        /// <summary>
        /// Parameterised Constructor
        /// </summary>
        /// <param name="paramGraphEntity">GraphEntity object</param>
        public GraphControl(GraphEntity paramGraphEntity)
        {
            View = new GraphView();
            View.GraphEnt = paramGraphEntity;            
        }

        /// <summary>
        /// Create a new graph series
        /// </summary>
        /// <param name="paramCounter">Performance Counter Object</param>
        /// <returns>GraphSeries object</returns>
        public GraphSeries CreatePerformanceGraph(PerfCounter paramCounter)
        {
            PerformanceData data = new PerformanceData(paramCounter);

            var filteredData = new FilteringDataSource<PerformanceInfo>(data, new MaxSizeFilter());

            var ds = new EnumerableDataSource<PerformanceInfo>(data);
            ds.SetXMapping(pi => dateAxis.ConvertToDouble(pi.Time));//.TimeOfDay.TotalSeconds);
            ds.SetYMapping(pi => pi.Value);

            LineGraph chart = customgraph.AddLineGraph(ds, 2.0, String.Format("{0} - {1} - {2} ", paramCounter.MachineName, paramCounter.Category, paramCounter.CounterName));
            

            //chart.DataChanged += chart_DataChanged;
            return new GraphSeries(data, chart);
        }

        private void AddCounterToGraph(object sender, MouseButtonEventArgs e)
        {
            //PerfCounter cntr1 = new PerfCounter("WorkerRole1_IN_0", "Processor", "% Processor Time", "_Total");
            //PerfCounter cntr2 = new PerfCounter("WorkerRole1_IN_0", "Processor", "% User Time", "_Total");
            //PerfCounter cntr3 = new PerfCounter("WebRole1_IN_0", "Processor", "% Processor Time", "_Total");
            //PerfCounter cntr4 = new PerfCounter("WebRole1_IN_0", "Processor", "% User Time", "_Total");

            //GraphSeries series1 = CreatePerformanceGraph(cntr1);
            //GraphSeries series2 = CreatePerformanceGraph(cntr2);
            //GraphSeries series3 = CreatePerformanceGraph(cntr3);
            //GraphSeries series4 = CreatePerformanceGraph(cntr4);

            //View.GraphEnt.GraphContext.Add(cntr1, series1);
            //View.GraphEnt.GraphContext.Add(cntr2, series2);
            //View.GraphEnt.GraphContext.Add(cntr3, series3);
            //View.GraphEnt.GraphContext.Add(cntr4, series4);
            ConfigureGraphControl();
        }

        private void RemoveCounterFromGraph(object sender, MouseButtonEventArgs e)
        {
            
        }

        private void DeleteCounterGraph(object sender, MouseButtonEventArgs e)
        {
            
        }

        /// <summary>
        /// Configure graph control
        /// </summary>
        /// <returns></returns>
        public bool ConfigureGraphControl()
        {
            GraphCounterConfig graphCntrConf = new GraphCounterConfig(this);
            graphCntrConf.ShowDialog();
            if (GraphCounterConfig.IsOK)
            {
                List<PerfCounter> CntrRemove = new List<PerfCounter>();
                //List<PerfCounter> CntrAdd = new List<PerfCounter>();

                foreach (PerfCounter cntr in View.GraphEnt.GraphContext.Keys)
                {
                    if (!GraphCounterConfig.SelectedGraphCounters.Contains(cntr))
                    {
                        CntrRemove.Add(cntr);
                    }
                }

                foreach (PerfCounter cntr in GraphCounterConfig.SelectedGraphCounters)
                {
                    if (!View.GraphEnt.GraphContext.Keys.Contains(cntr))
                    {
                        GraphSeries series = CreatePerformanceGraph(cntr);
                        View.GraphEnt.GraphContext.Add(cntr, series);
                    }
                }

                foreach (PerfCounter cntr in CntrRemove)
                {                    
                    customgraph.Children.Remove(View.GraphEnt.GraphContext[cntr].Chart);
                    View.GraphEnt.GraphContext.Remove(cntr);                   
                }
                GraphCounterConfig.SelectedGraphCounters.Clear();
                GraphCounterConfig.IsOK = false;
                return true;
            }
            
            GraphCounterConfig.SelectedGraphCounters.Clear();
            GraphCounterConfig.IsOK = false;
            return false;
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {

        }

    }
}
